/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.Answer;
import models.Question;
import models.Test;
import models.TestResult;
import models.User;
import org.hibernate.Session;
import service.HibernateUtil;
import service.ProgressTestDao;
import service.UserDao;

/**
 *
 * @author Aster
 */
public class ProgressTestControllerStudent extends HttpServlet {

    HibernateUtil HU = new HibernateUtil();
    Session session = HU.openSession();
    ProgressTestDao pdao = new ProgressTestDao(session);
    UserDao udao = new UserDao(session);

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession sessie = request.getSession(true);

        if (request.getParameter("sc") != null) {
            List<Test> allTest = pdao.findAllTests(Long.parseLong(request.getParameter("sc")));
            sessie.setAttribute("courseTests", allTest);
        }
        if (request.getParameter("st") != null) {
            Long st = Long.parseLong(request.getParameter("st"));
            Test t = pdao.getTest(st);
            
            if (request.getParameter("type").equals("overzicht")) { //get grades
                String userID = "";
                for (Cookie cookie : request.getCookies()) {
                    if (cookie.getName().equals("userID")) {
                        userID = cookie.getValue();
                    }
                }
                User user = udao.get(Long.parseLong(userID));
                List<TestResult> results = pdao.getTestResults(t, user);
                request.setAttribute("results", results);                

            } else { //doe test, get questions
                if (request.getParameter("type").equals("participate")) {
                    Set<Question> allQuestions = t.getQuestions();
                    sessie.setAttribute("selectedTest", t);
                    request.setAttribute("testQuestions", allQuestions);
                }
            }
        }
        forward(request, response, "studentprogresstest.jsp");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession sessie = request.getSession(true);
        Test test = (Test) sessie.getAttribute("selectedTest");

        if (request.getParameter("type").equals("grade")) {
            int aantalGoed = 0;
            Set<Question> allQuestions = test.getQuestions();
            for (Question q : allQuestions) {
                String answerID = request.getParameter(String.valueOf(q.getQuestionID()));
                Answer a = pdao.getAnswer(Long.parseLong(answerID));
                if (a.getCorrect().equals("true")) {
                    aantalGoed++;
                }
            }
            float grade = ((float) aantalGoed / (float) allQuestions.size()) * 100;

            //gebruiker ophalen
            String userID = "";
            for (Cookie cookie : request.getCookies()) {
                if (cookie.getName().equals("userID")) {
                    userID = cookie.getValue();
                }
            }
            User user = udao.get(Long.parseLong(userID));
            Date dNow = new Date();

            SimpleDateFormat ft =
               new SimpleDateFormat("dd.MM.yyyy 'at' hh:mm:ss");
            TestResult tr = new TestResult(test, user, grade, dNow);
            pdao.saveTestResult(tr);
            request.setAttribute("grade", grade);
            sessie.setAttribute("selectedTest", null);
            forward(request, response, "studentprogresstest.jsp");
        }
    }

    protected void forward(HttpServletRequest request, HttpServletResponse response, String url)
            throws ServletException, IOException {
        String address = "/" + url;
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}